/**
 * Created by douxin on 15/4/29.
 */
$("#uploadModal").on("hidden.bs.modal", function () {
    $("#selected_files").empty();
    $("#appName").val('');
    $(".progress-bar").css("width", "0%").val('0%');
    $(".row#progress").hide();
    console.log($("#fileupload").find('files'));
});

function human_read(size) { // size in bytes
    var ext;
    var result = size / 1024;
    var flag = 1;
    while (result > 100) {
        result = result / 1024;
        ++flag;
    }
    switch (flag) {
        case 1:
            ext = "Kb";
            break;
        case 2:
            ext = "Mb";
            break;
        case 3:
            ext = "Gb";
            break;
        case 4:
            ext = "Tb";
            break;
    }
    return result.toFixed(2) + " " + ext;
}

//$("#fileupload").fileupload({
//    sequentialUploads: true,
//    singleFileUploads: false,
//    autoUpload: false
//}).bind("fileuploadadd", function (e, data) {
//    $.each(data.files, function(index, file) {
//        $("#selected_files").append(
//            '<div class="row">' +
//            '<div class="col-xs-3 long-truncate text-justify">' + file.name + '</div>' +
//            '<div class="col-xs-2 text-right">' + human_read(file.size) + '</div>' +
//            '<div class="col-xs-6 text-justify">' +
//            '<div class="progress">' +
//            '<div id="' + index + '" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;min-width: 2em;">' +
//            '0%</div>' +
//            '</div>' +
//            '</div>' +
//                //'<div class="col-xs-1">' +
//                //    '<button onclick="delete_file(this)" class="close" type="button" aria-label="Close"><span aria-hidden="true" class="glyphicon glyphicon-remove"></span></button>' +
//                //'</div>' +
//            '</div>'
//        );
//    });
//    data.submit();
//}).bind("fileuploadprogress", function (e, data) {
//    console.log(data);
//    var progress = parseInt(data.loaded / data.total * 100, 10);
//    $(".progress-bar").css("width", progress + "%").text(progress + '%');
//});

//$("#startUpload").click(function () {
//    var appName = $("#appName").val();
//    if (appName === ''){
//        alert("请输入app名称");
//        return;
//    }
//    //console.log(file_upload);
//    console.log(file_list);
//    //elem.fileupload('send', {files: file_list, formData: {"app_name": appName}});
//});

$("#fileupload").fileupload({
    //option: {
    sequentialUploads: true,
    singleFileUploads: false,
    autoUpload: false,
    //},
    add: function (e, data) {
        //data.url = "/workstation/app/";
        $.each(data.files, function (index, file) {
            $("#selected_files").append(
                '<div class="row" style="margin-top: 8px; position: relative">' +
                '<div class="col-xs-6 long-truncate text-justify">' + file.name + '</div>' +
                '<div class="col-xs-6 text-right">' + human_read(file.size) + '</div>' +
                //'<div class="col-xs-6 text-justify">' +
                //'<div class="progress">' +
                //'<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;min-width: 2em;">' +
                //'0%</div>' +
                //'</div>' +
                //'</div>' +
                    //'<div class="col-xs-1">' +
                    //    '<button onclick="delete_file(this)" class="close" type="button" aria-label="Close"><span aria-hidden="true" class="glyphicon glyphicon-remove"></span></button>' +
                    //'</div>' +
                '</div>'
            );
        });
        $(".row#progress").show();
        data.context = $("#startUpload").click(function () {
            var appName = $("#appName").val();
            if (appName === '') {
                alert("请输入app名称");
                return;
            }
            data.formData = {'app_name': appName};
            data.submit();
        });
    },
    progress: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $(".progress-bar").css("width", progress + "%").text(progress + '%');
    }
});
